package com.itheima.fengkuanglianxi.bookmanager.bookdao;

import com.itheima.fengkuanglianxi.bookmanager.domain.Book;

public class BookDao implements BaseBookDao {

    private Book[] books = new Book[5];

    public Book[] getAllBooks() {

        return books;

    }

    public boolean addBook(Book book) {

        for (int i = 0; i < books.length; i++) {
            Book bookArray = books[i];
            if(bookArray == null){
                books[i] = book;
                return true;
            }
        }
        return false;

    }

    public void deleteBook(String bookId) {
        for (int i = 0; i < books.length; i++) {
            Book bookArray = books[i];
            if(bookArray != null){
                if(bookArray.getBookId().equals(bookId)){
                    books[i] = null;
                }
            }
        }
    }

    public void updateBook(Book book) {
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            Book bookArray = books[i];
            if(bookArray != null){
                if(bookArray.getBookId().equals(book.getBookId())){
                    index = i;
                    break;

                }
            }
        }
        if(index != -1){
            books[index] = book;
        }

    }
}
